<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Config;
use think\Db;
use think\Session;

/**
 * 控制台
 *
 * @icon fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{

    /**
     * 查看
     */
    public function index()
    {
        $seventtime = \fast\Date::unixtime('day', -7);
        $paylist = $createlist = [];
        for ($i = 0; $i < 7; $i++)
        {
            $day = date("Y-m-d", $seventtime + ($i * 86400));
            $createlist[$day] = mt_rand(20, 200);
            $paylist[$day] = mt_rand(1, mt_rand(1, $createlist[$day]));
        }
        $hooks = config('addons.hooks');
        $uploadmode = isset($hooks['upload_config_init']) && $hooks['upload_config_init'] ? implode(',', $hooks['upload_config_init']) : 'local';
        $addonComposerCfg = ROOT_PATH . '/vendor/karsonzhang/fastadmin-addons/composer.json';
        Config::parse($addonComposerCfg, "json", "composer");
        $config = Config::get("composer");
        $addonVersion = isset($config['version']) ? $config['version'] : __('Unknown');
        $adminInfo = Session::get('admin');
        $site_id =$adminInfo['site_id'];
        $whereSite = [];
        if($site_id){
            $whereSite['site_id'] = $site_id;
        }

        $article = Db::name('article')->where($whereSite)->count('id');
        $todayArticle = Db::name('article')->where($whereSite)->whereTime('createtime','today')->count('id');

        $asking = Db::name('asking')->where($whereSite)->count('id');
        $todayAsking = Db::name('asking')->where($whereSite)->whereTime('createtime','today')->count('id');

        $campaign = Db::name('vote')->where('publishdata','1')->where('status','1')->where('typedata','1')->where($whereSite)->count('id');
        $campaigning =Db::name('vote')->where('publishdata','1')->where('status','1')->where('typedata','1')->where('endtime','<',time())->where($whereSite)->count('id');

        $deliberate = Db::name('vote')->where('publishdata','1')->where('status','1')->where('typedata','2')->where($whereSite)->count('id');
        $deliberating =Db::name('vote')->where('publishdata','1')->where('status','1')->where('typedata','2')->where('endtime','<',time())->where($whereSite)->count('id');

        $active = Db::name('vote')->where('publishdata','1')->where('status','1')->where('typedata','3')->where($whereSite)->count('id');
        $activing =Db::name('vote')->where('publishdata','1')->where('status','1')->where('typedata','3')->where('endtime','<',time())->where($whereSite)->count('id');

        $siteCheck = Db::name('room_check')->where('typedata','2')->where('status','1')->where($whereSite)->count('id');
        $userCheck = Db::name('room_check')->where('typedata','1')->where('status','1')->where($whereSite)->count('id');

        $user = Db::name('user')->count('id');
        $checkUser = Db::name('user')->where('is_check','1')->where('checkdata',1)->count('id');
        $siter = Db::name('user')->where('is_check','1')->where('is_siter',1)->count('id');

        $this->view->assign([
            'totalUser'        => $user,
            'checkUser'        => $checkUser,
            'siter'            => $siter,
            'siteCheck'        => $siteCheck,
            'userCheck'        => $userCheck,
            'article'          => $article,
            'todayArticle'     => $todayArticle,
            'asking'           => $asking,
            'todayAsking'      => $todayAsking,
            'campaign'         => $campaign,
            'campaigning'      => $campaigning,
            'deliberate'       => $deliberate,
            'deliberating'     => $deliberating,
            'active'           => $active,
            'activing'         => $activing,
            'site_id'          => $site_id,
            'paylist'          => $paylist,
            'createlist'       => $createlist,
            'addonversion'     => $addonVersion,
            'uploadmode'       => $uploadmode
        ]);

        return $this->view->fetch();
    }

}
